package com.xiao.gulimall.search.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.xiao.gulimall.search.config.GuliESConfig;
import com.xiao.gulimall.search.vo.SearchParam;
import com.xiao.gulimall.search.vo.SearchResult;
import com.xiao.gulimall.search.vo.SkuEsModel;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class IndexWeb {
    @Autowired
    private RestHighLevelClient client;

    @GetMapping(value = {"/","/search.html"})
    public String searchhtml(Model model,
                             SearchParam searchParam,
                             HttpServletRequest request) throws IOException {
        searchParam.set_queryString(request.getQueryString());
        //查询条件
        SearchRequest searchRequest = new SearchRequest("product01a");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        //keyword
        if(!StringUtils.isBlank(searchParam.getKeyword())){
            queryBuilder.must(QueryBuilders.matchQuery("skuTitle",searchParam.getKeyword()));
        }
        //catalog3Id
        if(searchParam.getCatalog3Id()!=null){
            queryBuilder.filter(QueryBuilders.termQuery("catalogId",searchParam.getCatalog3Id()));
        }
        //品牌查询
        if(searchParam.getBrandId()!=null){
            queryBuilder.filter(QueryBuilders.termsQuery("brandId",searchParam.getBrandId()));
        }
        //价格区间
        if(searchParam.getSkuPrice()!=null && searchParam.getSkuPrice()!=""){
            String[] split = searchParam.getSkuPrice().split("_");
            //100_900  _900  100_
            if(searchParam.getSkuPrice().startsWith("_")){
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").lte(split[1]));
            }else if(searchParam.getSkuPrice().endsWith("_")){
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").gte(split[0]));
            }else{
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").lte(split[1]).gte(split[0]));
            }
        }
        //显示有货
        queryBuilder.filter(QueryBuilders.termQuery("hasStock",searchParam.isHasStock()));
        //规格参数
        if(searchParam.getAttrs()!=null && searchParam.getAttrs().size()>0){
//            // aa "7_麒麟880:麒麟990"
//            //2_2020
            for (String aa : searchParam.getAttrs()) {
//                //"7_麒麟880:麒麟990"
                String[] s = aa.split("_");
                List<String> attrs = searchParam.getAttrs();
                BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
                boolQuery.must(QueryBuilders.termQuery("attrs.attrId",s[0]));
//               String[] split = s[1].split(":");
//                boolQuery.must(QueryBuilders.termQuery("attrs.attrName",split[1]));
                //contains:是判断下面是否还有字符串，如果有返回true，没有返回false
                if(s[1].contains(":")){
                    String[] split = s[1].split(":");
                    boolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",split));
                }else{
                    boolQuery.must(QueryBuilders.termQuery("attrs.attrValue",s[1]));
                }
                NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("attrs", boolQuery, ScoreMode.None);
                queryBuilder.filter(nestedQueryBuilder);
            }
        }
        //查询条件的总结
        sourceBuilder.query(queryBuilder);
        //分页
        sourceBuilder.from(1);
        sourceBuilder.size(10);
        //排序
        if(searchParam.getSort()!=null){
            String[] s = searchParam.getSort().split("_");
            if(s[1].equals("desc")||s[1].equals("DESC")){
                sourceBuilder.sort(s[0], SortOrder.DESC);
            }else{
                sourceBuilder.sort(s[0], SortOrder.ASC);
            }
        }
        //高量
        if(!StringUtils.isBlank(searchParam.getKeyword())){
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field highlightTitle =
                    new HighlightBuilder.Field("skuTitle");
            //给highlightTitle设置查询规范
            highlightTitle.highlighterType("unified");
            highlightTitle.preTags("<span style='color:red'>");
            highlightTitle.postTags("</span>");
            //给highlightBuilder赋值规范
            highlightBuilder.field(highlightTitle);
            //赋值给进入es中查询的sourceBuilder
            sourceBuilder.highlighter(highlightBuilder);
        }
        //品牌分组
        TermsAggregationBuilder terms = AggregationBuilders.terms("brandAgg");
        terms.field("brandId");
        terms.subAggregation(AggregationBuilders.terms("brandNameAgg").field("brandName"));
        terms.subAggregation(AggregationBuilders.terms("brandImgAgg").field("brandImg"));
        sourceBuilder.aggregation(terms);
        //分类分组
        TermsAggregationBuilder catalogAgg = AggregationBuilders.terms("catalogAgg");
        catalogAgg.field("catalogId");
        catalogAgg.subAggregation(AggregationBuilders.terms("catalogNameAgg").field("catalogName"));
        sourceBuilder.aggregation(catalogAgg);
        //属性
        NestedAggregationBuilder nested = AggregationBuilders.nested("attrs", "attrs");
        TermsAggregationBuilder attrIdAgg = AggregationBuilders.terms("attrIdAgg").field("attrs.attrId");
        TermsAggregationBuilder attrName01 = AggregationBuilders.terms("attrNameAgg").field("attrs.attrName");
        TermsAggregationBuilder attrValue02 = AggregationBuilders.terms("attrValueAgg").field("attrs.attrValue");

        nested.subAggregation(attrValue02);
        nested.subAggregation(attrName01);
        nested.subAggregation(attrIdAgg);
        sourceBuilder.aggregation(nested);
        //把查询资源放入查询球请求中
        //请求数据
        searchRequest.source(sourceBuilder);
        System.out.println(searchRequest);
        //执行
        //响应数据。
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println("==============="+searchResponse);
        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
        long total = totalHits.value;
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();

        List<SkuEsModel> list = new ArrayList<>();

        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            SkuEsModel skuEsModel = JSON.parseObject(sourceAsString, new TypeReference<SkuEsModel>() {
            });
            skuEsModel.setSkuId(Long.parseLong(id));
            //处理高量
            if (!StringUtils.isBlank(searchParam.getKeyword())) {
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField highlight = highlightFields.get("skuTitle");
                System.out.println(highlight.fragments());
                Text[] fragments = highlight.fragments();
                String fragmentString = fragments[0].string();
                skuEsModel.setSkuTitle(fragmentString);
            }
            list.add(skuEsModel);
        }
        //品牌分组
        Aggregations aggregations = searchResponse.getAggregations();
        Terms brandAgg = aggregations.get("brandAgg");
        System.out.println(brandAgg.getBuckets());
        List<? extends Terms.Bucket> buckets = brandAgg.getBuckets();

        //   List<SearchResult.BrandVo> brandVos = new SearchResult().getBrands();
        ArrayList<SearchResult.BrandVo> brandVos = new ArrayList<>();
        for (Terms.Bucket bucket : buckets) {
            //创建品牌vo
            SearchResult.BrandVo brandVo = new SearchResult.BrandVo();
            //获取品牌ID
            String brandId = bucket.getKeyAsString();
            //获取品牌图片
            Terms brandImgAgg = bucket.getAggregations().get("brandImgAgg");
            Terms.Bucket bucket1 = brandImgAgg.getBuckets().get(0);
            String brandImg = bucket1.getKeyAsString();
            //获取品牌名称
            Terms nameAgg = bucket.getAggregations().get("brandNameAgg");
            Terms.Bucket bucket2 = nameAgg.getBuckets().get(0);
            String brandName = bucket2.getKeyAsString();
            //赋值brandvo
            brandVo.setBrandId(Long.parseLong(brandId));
            brandVo.setBrandImg(brandImg);
            brandVo.setBrandName(brandName);
            brandVos.add(brandVo);
        }
        //分类分组
        Terms cataloggroup = aggregations.get("catalogAgg");
        List<? extends Terms.Bucket> cataloglist = cataloggroup.getBuckets();
        //    List<SearchResult.CatalogVo> catalogVos = new SearchResult().getCatalogs();
        ArrayList<SearchResult.CatalogVo> catalogVos = new ArrayList<>();
        for (Terms.Bucket catal : cataloglist) {
            SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo();
            String catalId = catal.getKeyAsString();
            Terms catalogNameAgg = catal.getAggregations().get("catalogNameAgg");
            Terms.Bucket bucket = catalogNameAgg.getBuckets().get(0);
            String catalName = bucket.getKeyAsString();
            catalogVo.setCatalogId(Long.parseLong(catalId));
            catalogVo.setCatalogName(catalName);
            catalogVos.add(catalogVo);
        }
        //规格属性
        ParsedNested attrs = aggregations.get("attrs");
        Terms attrId = attrs.getAggregations().get("attrIdAgg");
        List<? extends Terms.Bucket> idBuckets = attrId.getBuckets();
        List<SearchResult.AttrVo> attrVos = new ArrayList<>();
        for (Terms.Bucket bucket : idBuckets) {
            SearchResult.AttrVo attrVo = new SearchResult.AttrVo();
            String attId = bucket.getKeyAsString();
            Terms attrNameAgg = attrs.getAggregations().get("attrNameAgg");
            Terms.Bucket bucket1 = attrNameAgg.getBuckets().get(0);
            String attrName = bucket1.getKeyAsString();
            Terms attrValueAgg = attrs.getAggregations().get("attrValueAgg");
            List<? extends Terms.Bucket> bucket2 = attrValueAgg.getBuckets();
            ArrayList<String> attrValue = new ArrayList<>();
            for (Terms.Bucket bucket3 : bucket2) {
                String attrValues = bucket3.getKeyAsString();
                attrValue.add(attrValues);
            }
            attrVo.setAttrId(Long.parseLong(attId));
            attrVo.setAttrName(attrName);
            attrVo.setAttrValue(attrValue);
            attrVos.add(attrVo);
        }
        SearchResult searchResult = new SearchResult();
        searchResult.setProducts(list);
        searchResult.setBrands(brandVos);
        searchResult.setCatalogs(catalogVos);
        searchResult.setAttrs(attrVos);
        searchResult.setPageNum(1);
        searchResult.setSize(5);
        searchResult.setTotal(total);
        searchResult.setTotalPages(3);

        System.out.println(searchResult);

        model.addAttribute("result",searchResult);
        return "search";
    }

    //批量上架
    @PostMapping("up")
    public boolean up(@RequestBody List<SkuEsModel> list) throws IOException {
        BulkRequest request = new BulkRequest();
        for (SkuEsModel skuEsModel : list) {
            IndexRequest indexRequest = new IndexRequest("product01a");
            indexRequest.id(skuEsModel.getSkuId().toString());
            String s = JSON.toJSONString(skuEsModel);
            IndexRequest source = indexRequest.source(s, XContentType.JSON);
            request.add(source);
        }
        BulkResponse bulk = client.bulk(request, GuliESConfig.COMMON_OPTIONS);
        return true;
    }

    //检索
    @GetMapping("SearchTest")
    public void searchtest() throws IOException {
        SearchParam searchParam = new SearchParam();
        searchParam.setKeyword("华为");
        searchParam.setCatalog3Id(225L);
        searchParam.setSort("saleCount_desc");
        ArrayList<Long> brid = new ArrayList<>();
        brid.add(2L);
        brid.add(3L);
        searchParam.setBrandId(brid);
        searchParam.setSkuPrice("1000_7000");
        searchParam.setHasStock(true);
        ArrayList<String> attrsname = new ArrayList<>();
        attrsname.add("7_麒麟880:麒麟990");
        attrsname.add("2_2020");
        searchParam.setAttrs(attrsname);
        //查询条件
        SearchRequest searchRequest = new SearchRequest("product01a");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        //keyword
        if(!StringUtils.isBlank(searchParam.getKeyword())){
            queryBuilder.must(QueryBuilders.matchQuery("skuTitle",searchParam.getKeyword()));
        }
        //catalog3Id
        if(searchParam.getCatalog3Id()!=null){
            queryBuilder.filter(QueryBuilders.termQuery("catalogId",searchParam.getCatalog3Id()));
        }
        //品牌查询
        if(searchParam.getBrandId()!=null){
            queryBuilder.filter(QueryBuilders.termsQuery("brandId",searchParam.getBrandId()));
        }
        //价格区间
        if(searchParam.getSkuPrice()!=null && searchParam.getSkuPrice()!=""){
            String[] split = searchParam.getSkuPrice().split("_");
            //100_900  _900  100_
            if(searchParam.getSkuPrice().startsWith("_")){
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").lte(split[1]));
            }else if(searchParam.getSkuPrice().endsWith("_")){
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").gte(split[0]));
            }else{
                queryBuilder.filter(QueryBuilders.rangeQuery("skuPrice").lte(split[1]).gte(split[0]));
            }
        }
        //显示有货
        queryBuilder.filter(QueryBuilders.termQuery("hasStock",searchParam.isHasStock()));
        //规格参数
        if(searchParam.getAttrs()!=null || searchParam.getAttrs().size()>0){
//            // aa "7_麒麟880:麒麟990"
//            //2_2020
           for (String aa : searchParam.getAttrs()) {
//                //"7_麒麟880:麒麟990"
                String[] s = aa.split("_");
                List<String> attrs = searchParam.getAttrs();
                BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
                boolQuery.must(QueryBuilders.termQuery("attrs.attrId",s[0]));
//               String[] split = s[1].split(":");
//                boolQuery.must(QueryBuilders.termQuery("attrs.attrName",split[1]));
                //contains:是判断下面是否还有字符串，如果有返回true，没有返回false
                if(s[1].contains(":")){
                    String[] split = s[1].split(":");
                    boolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",split));
                }else{
                    boolQuery.must(QueryBuilders.termQuery("attrs.attrValue",s[1]));
                }
                NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("attrs", boolQuery, ScoreMode.None);
                queryBuilder.filter(nestedQueryBuilder);
            }
        }
        //查询条件的总结
        sourceBuilder.query(queryBuilder);
        //分页
        sourceBuilder.from(1);
        sourceBuilder.size(10);
        //排序
        if(searchParam.getSort()!=null){
            String[] s = searchParam.getSort().split("_");
            if(s[1].equals("desc")||s[1].equals("DESC")){
                sourceBuilder.sort(s[0], SortOrder.DESC);
            }else{
                sourceBuilder.sort(s[0], SortOrder.ASC);
            }
        }
        //高量
        if(!StringUtils.isBlank(searchParam.getKeyword())){
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field highlightTitle =
                    new HighlightBuilder.Field("skuTitle");
            //给highlightTitle设置查询规范
            highlightTitle.highlighterType("unified");
            highlightTitle.preTags("<span style='color:red'>");
            highlightTitle.postTags("</span>");
            //给highlightBuilder赋值规范
            highlightBuilder.field(highlightTitle);
            //赋值给进入es中查询的sourceBuilder
            sourceBuilder.highlighter(highlightBuilder);
        }
        //品牌分组
        TermsAggregationBuilder terms = AggregationBuilders.terms("brandAgg");
        terms.field("brandId");
        terms.subAggregation(AggregationBuilders.terms("brandNameAgg").field("brandName"));
        terms.subAggregation(AggregationBuilders.terms("brandImgAgg").field("brandImg"));
        sourceBuilder.aggregation(terms);
        //分类分组
        TermsAggregationBuilder catalogAgg = AggregationBuilders.terms("catalogAgg");
        catalogAgg.field("catalogId");
        catalogAgg.subAggregation(AggregationBuilders.terms("catalogNameAgg").field("catalogName"));
        sourceBuilder.aggregation(catalogAgg);
        //属性
        NestedAggregationBuilder nested = AggregationBuilders.nested("attrs", "attrs");
        TermsAggregationBuilder attrIdAgg = AggregationBuilders.terms("attrIdAgg").field("attrs.attrId");
        TermsAggregationBuilder attrName01 = AggregationBuilders.terms("attrNameAgg").field("attrs.attrName");
        TermsAggregationBuilder attrValue02 = AggregationBuilders.terms("attrValueAgg").field("attrs.attrValue");

        nested.subAggregation(attrValue02);
        nested.subAggregation(attrName01);
        nested.subAggregation(attrIdAgg);
        sourceBuilder.aggregation(nested);
        //把查询资源放入查询球请求中
        //请求数据
        searchRequest.source(sourceBuilder);
        System.out.println(searchRequest);
        //执行
        //响应数据。
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println("==============="+searchResponse);
        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
        long total = totalHits.value;
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();

        List<SkuEsModel> list = new ArrayList<>();

        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            SkuEsModel skuEsModel = JSON.parseObject(sourceAsString, new TypeReference<SkuEsModel>() {
            });
            skuEsModel.setSkuId(Long.parseLong(id));
            //处理高量
            if (!StringUtils.isBlank(searchParam.getKeyword())) {
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField highlight = highlightFields.get("skuTitle");
                System.out.println(highlight.fragments());
                Text[] fragments = highlight.fragments();
                String fragmentString = fragments[0].string();
                skuEsModel.setSkuTitle(fragmentString);
            }
            list.add(skuEsModel);
        }
        //品牌分组
        Aggregations aggregations = searchResponse.getAggregations();
        Terms brandAgg = aggregations.get("brandAgg");
        System.out.println(brandAgg.getBuckets());
        List<? extends Terms.Bucket> buckets = brandAgg.getBuckets();

     //   List<SearchResult.BrandVo> brandVos = new SearchResult().getBrands();
        ArrayList<SearchResult.BrandVo> brandVos = new ArrayList<>();
        for (Terms.Bucket bucket : buckets) {
            //创建品牌vo
            SearchResult.BrandVo brandVo = new SearchResult.BrandVo();
            //获取品牌ID
            String brandId = bucket.getKeyAsString();
            //获取品牌图片
            Terms brandImgAgg = bucket.getAggregations().get("brandImgAgg");
            Terms.Bucket bucket1 = brandImgAgg.getBuckets().get(0);
            String brandImg = bucket1.getKeyAsString();
            //获取品牌名称
            Terms nameAgg = bucket.getAggregations().get("brandNameAgg");
            Terms.Bucket bucket2 = nameAgg.getBuckets().get(0);
            String brandName = bucket2.getKeyAsString();
            //赋值brandvo
            brandVo.setBrandId(Long.parseLong(brandId));
            brandVo.setBrandImg(brandImg);
            brandVo.setBrandName(brandName);
            brandVos.add(brandVo);
        }
        //分类分组
        Terms cataloggroup = aggregations.get("catalogAgg");
        List<? extends Terms.Bucket> cataloglist = cataloggroup.getBuckets();
    //    List<SearchResult.CatalogVo> catalogVos = new SearchResult().getCatalogs();
        ArrayList<SearchResult.CatalogVo> catalogVos = new ArrayList<>();
        for (Terms.Bucket catal : cataloglist) {
            SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo();
            String catalId = catal.getKeyAsString();
            Terms catalogNameAgg = catal.getAggregations().get("catalogNameAgg");
            Terms.Bucket bucket = catalogNameAgg.getBuckets().get(0);
            String catalName = bucket.getKeyAsString();
            catalogVo.setCatalogId(Long.parseLong(catalId));
            catalogVo.setCatalogName(catalName);
            catalogVos.add(catalogVo);
        }
        //规格属性
        ParsedNested attrs = aggregations.get("attrs");
        Terms attrId = attrs.getAggregations().get("attrIdAgg");
        List<? extends Terms.Bucket> idBuckets = attrId.getBuckets();
        List<SearchResult.AttrVo> attrVos = new ArrayList<>();
        for (Terms.Bucket bucket : idBuckets) {
            SearchResult.AttrVo attrVo = new SearchResult.AttrVo();
            String attId = bucket.getKeyAsString();
            Terms attrNameAgg = attrs.getAggregations().get("attrNameAgg");
            Terms.Bucket bucket1 = attrNameAgg.getBuckets().get(0);
            String attrName = bucket1.getKeyAsString();
            System.out.println(attrName);
            Terms attrValueAgg = attrs.getAggregations().get("attrValueAgg");
            List<? extends Terms.Bucket> bucket2 = attrValueAgg.getBuckets();
            ArrayList<String> attrValue = new ArrayList<>();
            for (Terms.Bucket bucket3 : bucket2) {
                String attrValues = bucket3.getKeyAsString();
                attrValue.add(attrValues);
            }
            attrVo.setAttrId(Long.parseLong(attId));
            attrVo.setAttrName(attrName);
            attrVo.setAttrValue(attrValue);
            attrVos.add(attrVo);
        }
        SearchResult searchResult = new SearchResult();
        searchResult.setProducts(list);
        searchResult.setBrands(brandVos);
        searchResult.setCatalogs(catalogVos);
        searchResult.setAttrs(attrVos);
        searchResult.setPageNum(1);
        searchResult.setSize(5);
        searchResult.setTotal(total);
        searchResult.setTotalPages(3);

        System.out.println(searchResult);
    }

    public static void main(String[] args) {
        String str="7_麒麟880:麒麟990";
        String[] s = str.split("_");
        System.out.println(s[1]);
        String[] split = s[1].split(":");
        System.out.println(split[1]);
    }
}
